// 累加
const arr = [1, 2, 3, 4, 5];

const sum = arr.reduce((pre, cur) => pre + cur);
console.log(sum); // 15

// 求最大值
const max = arr.reduce((pre, cur) => pre > cur ? pre : cur);
console.log(max); // 5

//素组去重
const arr1 = [1, 2, 3, 4, 5, 1, 2, 3];
const newArr = arr1.reduce((pre, cur) => {
  if (!pre.includes(cur)) {
    pre.push(cur);
  }
  return pre;
}, []);
console.log(newArr); // [1, 2, 3, 4, 5]


//归类
const dataList= [{
  name: 'aa',
  country: "china"
},{
    name: 'bb',
    country: "china"
},{
    name: 'cc',
    country: "usa"
},{
    name: 'dd',
    country: "usa"
},{
    name: 'ee',
    country: "japan"
},{
    name: 'ff',
    country: "japan"
}]

const result = dataList.reduce((preObj, cur) => {
    const {country} = cur;
    if(!preObj[country]) {
        preObj[country] = []
    } 
    preObj[country].push(cur)
    return preObj
}, [])
console.log(result)

//字符串翻转
const str = 'hello world'
const nreStr = Array.from(str).reduce((pre, cur) => cur + pre)
console.log(nreStr)